﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
namespace Sale_Operation
{
    public class AccountListOp : Component
    {
        public override bool Save(object obj)
        {
            bool blRes = false;
            AccountList SM = obj as AccountList;
           
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SM.AList_Id == 0)
                {
                    sbSql.Append("INSERT INTO AccountList (AList_Name,AList_UserName,AList_Money,AList_BData_Id,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("AList_Name='" + ValueHandler.GetStringValue(SM.AList_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("AList_UserName='" + ValueHandler.GetStringValue(SM.AList_UserName, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("AList_Money='" + ValueHandler.GetStringValue(SM.AList_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("AList_BData_Id='" + ValueHandler.GetStringValue(SM.AList_BData_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.AList_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增银行账户信息[" + ValueHandler.GetStringValue(SM.AList_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.账户管理);

                }
                else
                {
                    sbSql.Append("UPDATE AccountList SET ");
                    sbSql.Append("AList_Name='" + ValueHandler.GetStringValue(SM.AList_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("AList_UserName='" + ValueHandler.GetStringValue(SM.AList_UserName, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("AList_BData_Id='" + ValueHandler.GetStringValue(SM.AList_BData_Id, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE AList_Id=" + SM.AList_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改银行账户信息[" + ValueHandler.GetStringValue(SM.AList_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.客户管理);
                }
                blRes = true;

            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改银行账户信息出错！[" + ex.ToString() + "]", EMenuList.账户管理);
            }
            db.Close();
            return blRes;
        }

        public override bool HasExists(object obj)
        {
            AccountList SM = obj as AccountList;         
            bool blRes = true;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM AccountList WHERE AList_Name='" + SM.AList_Name + "' AND AList_Id<>" + SM.AList_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
            db = new DataBase();
            bool blRes = false;
            try
            {
                AccountList SM = obj as AccountList;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" UPDATE AccountList SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE AList_Id=" + SM.AList_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除银行账户[" + ValueHandler.GetStringValue(SM.AList_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.账户管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除进货单信息！[" + ex.ToString() + "]", EMenuList.账户管理);
            }
            db.Close();

            return blRes; ;
        }

        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT AList_Id,AList_Name,AList_UserName,AList_Money,AList_BData_Id,B.BData_Name,A.State,A.CreateTime,A.CreateMan FROM AccountList A ");
                sbSql.Append(" LEFT JOIN BaseData B ON (A.AList_BData_Id=B.BData_Id)");
                sbSql.Append(" WHERE A.State= 1");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "银行账户列表加载失败！[" + ex.ToString() + "]", EMenuList.账户管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public override DataTable GetList(string strCondition)
        {
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT AList_Id,AList_Name,AList_UserName,AList_Money,AList_BData_Id,B.BData_Name,A.State,A.CreateTime,A.CreateMan,AList_Name+'['+B.BData_Name+']' AS AccountInfo FROM AccountList A ");
                sbSql.Append(" LEFT JOIN BaseData B ON (A.AList_BData_Id=B.BData_Id)");
                sbSql.Append(" WHERE A.State= 1");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] {"dtList" });
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "银行账户列表加载失败！[" + ex.ToString() + "]", EMenuList.账户管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }

        public override object GetModel(object obj)
        {
            AccountList SM = obj as AccountList;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT AList_Id,AList_Name,AList_UserName,AList_Money,AList_BData_Id,B.BData_Name,A.State,A.CreateTime,A.CreateMan FROM AccountList A ");
                sbSql.Append(" LEFT JOIN BaseData B ON (A.AList_BData_Id=B.BData_Id)");
                sbSql.Append(" WHERE AList_Id= " + SM.AList_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.AList_BData_Id = ValueHandler.GetIntNumberValue(dr["AList_BData_Id"]);
                    SM.AList_Money = ValueHandler.GetDecNumberValue(dr["AList_Money"]);
                    SM.AList_Name = ValueHandler.GetStringValue(dr["AList_Name"]);
                    SM.AList_UserName = ValueHandler.GetStringValue(dr["AList_UserName"]);
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "银行账户列表加载失败！[" + ex.ToString() + "]", EMenuList.账户管理);
            }
            db.Close();
            return SM;
        }
    }
}
